<?php

namespace app\personal\controller;

use think\Controller;
use think\Request;
use think\Db;

class Discuss extends Controller
{
    /**
     *   初始化
     *  判断是否登陆
     */
    public function _initialize()
    {
        if (session('tel') == null){
            return $this->error('您还未登陆,请先登陆!');
        }
    }

    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {


        $discuss = Db::table(['article', 'user_self','discuss'])
            ->field('article.aname,article.amage,discuss.time,discuss.content,discuss.did,user_self.nickname')
            ->where('user_self.uid=discuss.uid  and discuss.aid=article.aid and discuss.uid='.session('uid'))
            ->select();

        foreach ($discuss as $k=>$v){
            $reply = db('answer')->where('did',$v['did'])->count();
            $discuss[$k]['answer'] = $reply;
        }
//        dump($discuss);die;
        $answer = Db::table(['article', 'user_self','discuss','answer'])
            ->field('article.aname,article.amage,answer.times,discuss.content,discuss.did,user_self.nickname,answer.contente,answer.id')
            ->where('user_self.uid=answer.uid and answer.did=discuss.did and discuss.aid=article.aid and answer.uid='.session('uid'))
            ->order('answer.times desc')
            ->select();
        $ass = Db::name('subscribe s')
            ->join('column c','s.cid = c.cid')
            ->where('s.uid ='.session('uid'))
            ->limit(9)
            ->select();
        $user = Db::table('user_self')->field('nickname,icon')->where('uid ='.session('uid'))->find();
//        dump($answer);die;
        return view('Discuss/index', [
            'discuss' => $discuss,
            'answer' => $answer,
            'ass' => $ass,
            'user' => $user,
        ]);
    }
    /**
     * 显示评论
     *
     * @param  int  $id 评论 id
     * @return \think\Response
     */
    public function read($id)
    {

        $list = Db::table(['answer','user_self'])
            ->field('answer.contente,answer.times,user_self.nickname')
            ->where('answer.uid=user_self.uid and answer.did='.$id)
            ->order('answer.times asc')
            ->select();
        return json($list);
    }

    /**
     * 删除回复评论.
     *
     * @param  int  $id 回复id
     * @return \think\Response
     */
    public function del($id)
    {
        $result = Db::table('answer')->where('id ='.$id)->delete();
        if (!$result > 0){
            return $this->error('删除回复失败');
        }
        return json($result);
    }

    /**
     * 回复评论
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function update()
    {
        $data = request()->post();
        $rst = $this->validate(
            [
                'contente'  => $data['content'],
                'did' => $data['did'],
            ],
            [
                ['contente','require','内容不能为空!'],
                ['did','require|number','数据错误!'],
            ]);
        if(true !== $rst){
            // 验证失败 输出错误信息
            return $this->error($rst);
        }
        $info =[
            'contente'=>$data['content'],
            'uid'=>session('uid'),
            'did'=>$data['did'],
            'times'=>time(),
        ];
        $result = Db::table('answer')->insert($info);
        if ($result > 0){
            return $this->success('回复成功',url('/discuss'));
        } else {
            return $this->error('修改失败');
        }
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id 评论id
     * @return \think\Response
     */
    public function delete($id)
    {
        $result = Db::table('discuss')->where('did',$id)->delete();
        if (!$result > 0){
            return $this->error('删除评论失败');
        }
        $list = Db::table('answer')->where('did',$id)->delete();
        return json($result);
    }

    /**
     * 发表评论
     * @param  content  评论内容
     * @param  aid      文章aid
     * @return \think\Response
     */
    public function create()
    {
        $data = request()->post();
        $rst = $this->validate(
            [
                'content'  => $data['content'],
                'aid' => $data['aid'],
            ],
            [
                ['contente','require','内容不能为空!'],
                ['aid','require|number','数据错误!'],
            ]);
        if(true !== $rst){
            // 验证失败 输出错误信息
            return $this->error($rst);
        }
        $info = [
            'uid'=>session('uid'),
            'aid'=>$data['aid'],
            'content'=>$data['content'],
            'time'=>time(),
        ];
        $result = Db::table('discuss')->insertGetId($info);
        if (!$result >0){
            return $this->error('评论失败!');
        }
        $info['did'] = $result;
        $info['name'] = session('name');
        return json($info);
    }

    /**
     * 回复评论
     * @param  content  评论内容
     * @param  did      评论id
     * @return \think\Response
     */
    public function save()
    {
        $data = request()->post();
        $rst = $this->validate(
            [
                'contente'  => $data['content'],
                'did' => $data['did'],
            ],
            [
                ['contente','require','内容不能为空!'],
                ['did','require|number','数据错误!'],
            ]);
        if(true !== $rst){
            // 验证失败 输出错误信息
            return $this->error($rst);
        }
        $info = [
            'uid'=>session('uid'),
            'did'=>$data['did'],
            'contente'=>$data['contente'],
            'times'=>time(),
        ];
        $result = Db::table('answer')->insertGetId($info);
        if (!$result >0){
            return $this->error('回复评论失败!');
        }
        $info['name'] = session('name');
        return json($info);
    }
}
